package com.jcfk.eam.service.mould;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.mould.EamMouldPackageInfoDao;
import com.jcfk.eam.domain.dto.mould.EamMouldPackageInfoDTO;
import com.jcfk.eam.domain.po.mould.EamMouldPackageInfo;
import com.jcfk.eam.domain.query.mould.EamMouldPackageInfoQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 模具模板框数据
 *
 * @author ly
 * @email 522891237@gqq.com
 * @date 2025-08-12
 */
@Service
public class EamMouldPackageInfoService extends BaseService<EamMouldPackageInfoDao, EamMouldPackageInfo> {

    /**
     * EamMouldPackageInfo分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldPackageInfoDTO> queryByPage(DataPage<EamMouldPackageInfoDTO> page, EamMouldPackageInfoQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamMouldPackageInfo是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldPackageInfoDTO dto) {
        QueryWrapper<EamMouldPackageInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamMouldPackageInfo::getDeleted, 0);
        queryWrapper.lambda().eq(EamMouldPackageInfo::getMouldCode, dto.getMouldCode());
        if (StrUtil.isNotEmpty(dto.getId())) {
            queryWrapper.lambda().notIn(EamMouldPackageInfo::getMouldCode, dto.getMouldCode());
        }
        if (this.count(queryWrapper) > 0) {
            return RetResult.fail("已存在");
        }
        return RetResult.ok();
    }

    /**
     * 插入模具模板框数据
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldPackageInfoDTO dto) {

        // 保存
        EamMouldPackageInfo item = new EamMouldPackageInfo();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模具模板框数据
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldPackageInfoDTO dto) {

        // 保存
        EamMouldPackageInfo item = new EamMouldPackageInfo();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模具模板框数据(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldPackageInfoDTO dto) {

        EamMouldPackageInfo item = new EamMouldPackageInfo();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMouldPackageInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldPackageInfo item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取模具模板框数据信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldPackageInfoDTO get(String id) {
        // 查询数据
        EamMouldPackageInfo item = this.baseMapper.selectById(id);
        EamMouldPackageInfoDTO dto = new EamMouldPackageInfoDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldPackageInfoDTO>
     */
    public List<EamMouldPackageInfoDTO> getList(EamMouldPackageInfoQuery query) {
        // 查询条件
        QueryWrapper<EamMouldPackageInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(EamMouldPackageInfo::getDeleted, 0);
        qw.lambda().eq(query.getStatus() != null, EamMouldPackageInfo::getStatus, query.getStatus());
        // 查询数据
        List<EamMouldPackageInfo> list = super.list(qw);

        return convertList(list, EamMouldPackageInfoDTO.class);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldPackageInfoDTO>
     */
    public List<EamMouldPackageInfo> getPoList(EamMouldPackageInfoQuery query) {
        // 查询条件
        QueryWrapper<EamMouldPackageInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(EamMouldPackageInfo::getDeleted, 0);
        qw.lambda().eq(query.getStatus() != null, EamMouldPackageInfo::getStatus, query.getStatus());
        // 查询数据
        return super.list(qw);
    }

    /**
     * 根据主键获取模具模板框数据信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamMouldPackageInfo item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }

    /**
     * 批量更新使用状态
     */
    public void checkUpdateStatus(String id) {
        List<EamMouldPackageInfoDTO> useList = this.baseMapper.queryUseList(id);
        List<EamMouldPackageInfoDTO> noUseList = this.baseMapper.queryNoUseList(id);
        String lastUpdatedUserId = SecurityUtils.getUserId();
        String lastUpdatedUserName = SecurityUtils.getUserName();
        List<EamMouldPackageInfo> changeUserList = useList.stream().filter(item -> item.getStatus() == 0).map(item -> {
            EamMouldPackageInfo eamMouldPackageInfo = new EamMouldPackageInfo();
            eamMouldPackageInfo.setStatus(1);
            eamMouldPackageInfo.setId(item.getId());
            item.setLastUpdatedUserId(lastUpdatedUserId);
            item.setLastUpdateTime(new Date());
            item.setLastUpdatedUserName(lastUpdatedUserName);
            return eamMouldPackageInfo;
        }).collect(Collectors.toList());
        List<EamMouldPackageInfo> changeNoUserList = noUseList.stream().filter(item -> item.getStatus() == 1).map(item -> {
            EamMouldPackageInfo eamMouldPackageInfo = new EamMouldPackageInfo();
            eamMouldPackageInfo.setStatus(0);
            eamMouldPackageInfo.setId(item.getId());
            item.setLastUpdatedUserId(lastUpdatedUserId);
            item.setLastUpdateTime(new Date());
            item.setLastUpdatedUserName(lastUpdatedUserName);
            return eamMouldPackageInfo;
        }).collect(Collectors.toList());
        List<EamMouldPackageInfo> updateList = new ArrayList<>();
        updateList.addAll(changeUserList);
        updateList.addAll(changeNoUserList);
        if (!updateList.isEmpty()) {
            this.updateBatchById(updateList);
        }
    }

    /**
     * 更新模具模板框数据信息
     */
    public void updateMouldPackAge() {
        QueryWrapper<EamMouldPackageInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamMouldPackageInfo::getDeleted, 0);
        queryWrapper.lambda().eq(EamMouldPackageInfo::getIsMaintenance, 0);
        EamMouldPackageInfo eamMouldPackageInfo = new EamMouldPackageInfo();
        eamMouldPackageInfo.setIsMaintenance(1);
        eamMouldPackageInfo.setLastUpdatedUserId("BATCH");
        eamMouldPackageInfo.setLastUpdateTime(new Date());
        eamMouldPackageInfo.setLastUpdatedUserName(SecurityUtils.getUserName());
        this.update(eamMouldPackageInfo, queryWrapper);
    }
}

